package com.yuerlive.rainbow.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ScrollView;

import com.yuerlive.rainbow.R;

import java.lang.reflect.Constructor;

/**
 * Created by xiaoyi on 2017/7/19.
 */

public class AutoFixScrollView extends ScrollView {
    private int MAX_HEIGHT;


    public AutoFixScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public AutoFixScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.AutoFixScrollView);
        MAX_HEIGHT = array.getDimensionPixelOffset(R.styleable.AutoFixScrollView_maxHeight,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,400,getResources().getDisplayMetrics()));

        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int finalMax = Math.min(MAX_HEIGHT,height);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(finalMax,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    }
}
